let timePickerElement = document.getElementById("myTimePicker");
let timePickerElement1 = document.getElementById("myTimePicker1");
let timePickerElement2 = document.getElementById("myTimePicker2");
let inputbox = document.getElementById("inputbox");
let status1 = document.getElementById("status1");
let status2 = document.getElementById("status2");
let Yard = document.getElementById("button-yard");
let Livingroom = document.getElementById("button-living");
let Bedroom = document.getElementById("button-bed");
let but_admit = document.getElementById("button-admit");
let but_auto_rst = document.getElementById("button-auto-rst");
let but_submit = document.getElementById("button-submit");
let but_auto = document.getElementById("button-auto");
let but_reset = document.getElementById("button-reset");

let ya1 = "/Yard"; //地址拼接
let living = "/Livingroom"; //地址拼接
let bed = "/Bedroom"; //地址拼接
let restime = "/Get-restime"; //地址拼接
let but_statue = "/Get-butstatue"; //地址拼接
let admit_statue = "/Get-admitstatue"; //地址拼接
let admit_change = "/Post-admitchange";
let inputbox_change = "/Post-inputboxchange";
let timepick_change = "/Post-timepickchange";
let auto_open_time = "/Post-autoopentime";
let auto_close_time = "/Post-autoclosetime";
let auto_close_open = "/Post-autocloseopen";
let gain_reset = "/Post-reset";
let post_autorst = "/Post-autorst";
let gain_inittime = "/Get-inittime";

// let but0_statue = false;
// let but1_statue = false;
// let but2_statue = false;

var i = 1000;
function clock() {
  i = i - 1;
  if (i > 0) {
    setTimeout("clock();", 1000);
  } else {
    self.close();
  }
}
clock();

init = function () {
  let sendRequest = new XMLHttpRequest();
  sendRequest.open("GET", but_statue, true);
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    if (sendRequest.readyState === 4 && sendRequest.status === 200) {
      let str = sendRequest.responseText;
      let num = parseInt(str);
      console.log(num);

      if (1 == parseInt(str.slice(0, 1))) Yard.className = "toggle-button";
      else Yard.className = "toggle-button off";

      if (1 == parseInt(str.slice(1, 2)))
        Livingroom.className = "toggle-button";
      else Livingroom.className = "toggle-button off";

      if (1 == parseInt(str.slice(2, 3))) Bedroom.className = "toggle-button";
      else Bedroom.className = "toggle-button off";

      if (1 == parseInt(str.slice(3, 4))) {
        but_auto.className = "toggle-button";
        timePickerElement1.style.background = "#7fca81";
        timePickerElement2.style.background = "#7fca81";
      } else {
        timePickerElement1.style.background = "#555b6145"; //如果没有打开自动开关灯就显示灰色
        timePickerElement2.style.background = "#555b6145";
        but_auto.className = "toggle-button off";
      }

      if (1 == parseInt(str.slice(4, 5))) {
        but_auto_rst.checked = true;
        timePickerElement.style.background = "#4caf50";
      } else {
        but_auto_rst.checked = false;
        timePickerElement.style.background = "#555b6145";
      }
    }
  };

  let sendRequest1 = new XMLHttpRequest();
  sendRequest1.open("GET", restime, true);
  sendRequest1.send(null);
  sendRequest1.onreadystatechange = function () {
    if (sendRequest1.readyState === 4 && sendRequest1.status === 200) {
      let str = sendRequest1.responseText;
      timePickerElement.value = str.slice(0, 2) + ":" + str.slice(2, 4);
      timePickerElement1.value = str.slice(4, 6) + ":" + str.slice(6, 8);
      timePickerElement2.value = str.slice(8, 10) + ":" + str.slice(10, 12);
    }
  };
  let sendRequest2 = new XMLHttpRequest();
  sendRequest2.open("GET", admit_statue, true);
  sendRequest2.send(null);
  sendRequest2.onreadystatechange = function () {
    if (sendRequest2.readyState === 4 && sendRequest2.status === 200) {
      let str = sendRequest2.responseText;
      if (str == "true") but_admit.checked = true;
      else but_admit.checked = false;
      console.log(str);
    }
  };

  let sendRequest3 = new XMLHttpRequest();
  sendRequest3.open("GET", gain_inittime, true);
  sendRequest3.send(null);
  sendRequest3.onreadystatechange = function () {
    if (sendRequest3.readyState === 4 && sendRequest3.status === 200) {
      let str = sendRequest3.responseText;
      status1.textContent =
        "系统开启时间：" + str.slice(0, 2) + ":" + str.slice(2, 4);
      let day = new Date();

      status2.textContent =
        "系统已运行时间" +
        str.slice(4, 5) +
        "天" +
        (parseInt(day.getHours()) - parseInt(str.slice(0, 2)) >= 0
          ? parseInt(day.getHours()) - parseInt(str.slice(0, 2))
          : parseInt(day.getHours()) + 24 - parseInt(str.slice(0, 2))) +
        "时" +
        (parseInt(day.getMinutes()) - parseInt(str.slice(2, 4)) >= 0
          ? parseInt(day.getMinutes()) - parseInt(str.slice(2, 4))
          : parseInt(day.getMinutes()) + 60 - parseInt(str.slice(2, 4))) +
        "分";
    }
  };
};

document.addEventListener("DOMContentLoaded", init);
setInterval(init, 1000); // 间隔时间为2000毫秒（2秒）

but_auto_rst.addEventListener("change", () => {
  i = 15 * 1000;
  if (but_auto_rst.checked == true)
    timePickerElement.style.background = "#7fca81";
  else timePickerElement.style.background = "#555b6145";

  let sendRequest = new XMLHttpRequest();
  sendRequest.open("GET", post_autorst, true);
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    if (sendRequest.readyState === 4 && sendRequest.status === 200) {
    }
  };
});

but_reset.addEventListener("click", () => {
  i = 15 * 100;
  let sendRequest = new XMLHttpRequest();
  sendRequest.open("GET", gain_reset, true);
  alert("系统即刻重启");
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    if (sendRequest.readyState === 4 && sendRequest.status === 200) {
    }
  };
});

but_auto.addEventListener("click", () => {
  i = 15 * 1000;
  var result = but_auto.getAttribute("class");
  if (result == "toggle-button") {
    timePickerElement1.style.background = "#555b6145"; //关闭打开自动开关灯 时间选择框显示灰色
    timePickerElement2.style.background = "#555b6145";
    but_auto.className = "toggle-button off";
  } else {
    but_auto.className = "toggle-button";
    timePickerElement1.style.background = "#7fca81"; //打开自动开关灯 时间选择框显示绿色
    timePickerElement2.style.background = "#7fca81";
  }
  let sendRequest = new XMLHttpRequest();
  sendRequest.open("POST", auto_close_open, true);
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    if (sendRequest.readyState === 4 && sendRequest.status === 200) {
    }
  };
});

timePickerElement1.addEventListener("input", () => {
  i = 15 * 1000;
  let timeStr = timePickerElement1.value;
  timeStr = timeStr.slice(0, 2) + timeStr.slice(3, 5);
  let sendRequest = new XMLHttpRequest();
  sendRequest.open("POST", auto_open_time + "?autoopentime=" + timeStr, true);
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    console.log(timeStr);
  };
});
timePickerElement2.addEventListener("input", () => {
  i = 15 * 1000;
  let timeStr = timePickerElement2.value;
  timeStr = timeStr.slice(0, 2) + timeStr.slice(3, 5);
  let sendRequest = new XMLHttpRequest();
  sendRequest.open("POST", auto_close_time + "?autoclosetime=" + timeStr, true);
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    console.log(timeStr);
  };
});

timePickerElement.addEventListener("input", function () {
  i = 15 * 1000;
  let timeStr = timePickerElement.value;
  timeStr1 = timeStr.slice(0, 2) + timeStr.slice(3, 5);
  let sendRequest = new XMLHttpRequest();
  sendRequest.open("POST", timepick_change + "?restime=" + timeStr1, true);
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    alert("系统将于每日" + timeStr + "重启");
  };
});

but_admit.addEventListener("change", () => {
  i = 15 * 1000;
  let isChecked = but_admit.checked;
  if (isChecked) {
    console.log("开关已打开" + isChecked);
  } else {
    console.log("开关已关闭" + isChecked);
  }
  let sendRequest = new XMLHttpRequest();
  sendRequest.open("POST", admit_change + "?admit=" + isChecked, true);
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    alert("已提交");
  };
});

but_submit.addEventListener("click", () => {
  i = 15 * 1000;
  let value = inputbox.value;

  let sendRequest = new XMLHttpRequest();
  sendRequest.open("POST", inputbox_change + "?auth=" + value, true);
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    alert("已提交" + value);
  };
});

Yard.addEventListener("click", () => {
  i = 15 * 1000;
  var result = Yard.getAttribute("class");
  if (result == "toggle-button") Yard.className = "toggle-button off";
  else Yard.className = "toggle-button";
  console.log("YAARD1");
  let sendRequest = new XMLHttpRequest();
  sendRequest.open("POST", ya1, true);
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    if (sendRequest.readyState === 4 && sendRequest.status === 200) {
    }
  };
});
Livingroom.addEventListener("click", () => {
  i = 15 * 1000;
  var result = Livingroom.getAttribute("class");
  if (result == "toggle-button") Livingroom.className = "toggle-button off";
  else Livingroom.className = "toggle-button";
  console.log("living");
  let sendRequest = new XMLHttpRequest();
  sendRequest.open("POST", living, true);
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    if (sendRequest.readyState === 4 && sendRequest.status === 200) {
    }
  };
});
Bedroom.addEventListener("click", () => {
  i = 15 * 1000;

  var result = Bedroom.getAttribute("class");
  if (result == "toggle-button") Bedroom.className = "toggle-button off";
  else Bedroom.className = "toggle-button";
  console.log("bed");
  let sendRequest = new XMLHttpRequest();
  sendRequest.open("POST", bed, true);
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    if (sendRequest.readyState === 4 && sendRequest.status === 200) {
    }
  };
});
